<?php

namespace App\Utils;

class UrlUtil
{
    /**
     * 获取当前环境的域名
     * @return string
     */
    public static function getHost(): string
    {
        $env = env('APP_ENV');
        
        if ($env === 'production') {
            return 'https://interview.seekcrew.com';
        }
        
        return 'https://interview.newstart2025.com';
    }

    /**
     * 获取当前船员端的域名
     * @return string
     */
    public static function getUserHost(): string
    {
        $env = config('app.env');
        
        if ($env === 'production') {
            return 'https://user-interview.seekcrew.com';
        }
        
        return 'http://user-interview.newstart2025.com';
    }

    /**
     * 获取面试报告url
     * @param int $id
     * @param array $params 额外的URL参数
     * @return string
     */
    public static function getInterviewReportUrl(int $id, array $params = []): string
    {
        // 白名单过滤非法参数
        $legalParams = ['type', 'token'];
        $params = array_intersect_key($params, array_flip($legalParams));
        $urlParams = array_merge([
            'id' => base64_encode($id)
        ], $params);
        
        return self::getHost() . '/admin/index.php?r=interview%2Freport&' . http_build_query($urlParams);
    }

    /**
     * 获取面试邀请url
     * @param array $params 额外的URL参数
     * @return string
     */
    public static function getInterviewUrl(array $params = []): string
    {
        // 白名单过滤非法参数
        $legalParams = ['admin_id', 'recruitment_id', 'position_id', 'user_id', 'utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content'];
        $params = array_intersect_key($params, array_flip($legalParams));
        
        return self::getHost() . '/index.php?r=interview%2Findex&' . http_build_query($params);
    }

    /**
     * 获取船员段招聘需求url
     * @param array $params 额外的URL参数
     * @return string
     */
    public static function getCrewRecruitmentUrl(array $params = []): string
    {
        // 白名单过滤非法参数
        $legalParams = ['id', 'admin_id', 'recruitment_id', 'position_id', 'user_id', 'utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content'];
        $params = array_intersect_key($params, array_flip($legalParams));
        return self::getUserHost() . '/position/detail?' . http_build_query($params);
    }

    /**
     * 获取船员端首页url
     * @param array $params 额外的URL参数
     * @return string
     */
    public static function getCrewHomeUrl(array $params = []): string
    {
        // 白名单过滤非法参数
        $legalParams = ['admin_id','recruitment_id', 'position_id', 'user_id', 'utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content'];
        $params = array_intersect_key($params, array_flip($legalParams));
        return self::getUserHost(). '/mine?'. http_build_query($params);
    }

    /**
     * 获取船员端登录url
     * @param array $params 额外的URL参数
     * @return string
     */
    public static function getCrewLoginUrl(array $params = []): string
    {
        return self::getUserHost(). '/login';   
    }

    /**
     * 获取客服url
     * @param array $params 额外的URL参数
     * @return string
     */
    public static function getChatUrl(): string
    {
        $env = env('APP_ENV');
        
        if ($env === 'production') {
            return 'http://chat-interview.seekcrew.com';
        }
        
        return 'http://chat-interview.newstart2025.com';
    }
}